﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.Training.Presenter.ViewInterfaces;
using System.Data;
using Truextend.Training.Model.Entities;
using Truextend.Training.Business;
using Truextend.Training.Service;
using Truextend.Training.Business.Dtos;
using Microsoft.SharePoint;

namespace Truextend.Training.Presenter
{
    public class ListCourseInstancePresenter
    {
        private IListCourseInstanceView view;

        public ListCourseInstancePresenter(IListCourseInstanceView view)
        {
            if (view == null) throw new ArgumentNullException("View may not be null");
            this.view = view;
        }

        public void InitView(int userId)
        {
            try
            {
                view.Message = "Initial Message";
                List<CourseInstanceRegisterDto> courseInstancesList = new CourseInstanceService().GetAllCourseInstancesRegisterAvailables().ToList();
                List<UserCourse> userCoursesList = new UserCourseService().GetAllUserCourses(userId).ToList();
                view.CourseInstances = courseInstancesList;
                view.UserCourses = userCoursesList;
            }
            catch (Exception e)
            {
                view.Message = e.Message;
            }
        }

        public bool Register(int userId, long courseInstanceId)
        {
            try
            {
                UserCourse userCourse = new UserCourse();
                userCourse.UserId = userId;
                userCourse.RegisterDate = DateTime.Today;

                userCourse = new UserCourseService().AddUserCourse(userCourse, courseInstanceId);
                return (userCourse != null);
            }
            catch (Exception e)
            {
                view.Message = e.Message;
            }
            return false;
        }

        public void SendEMail(string userEmail, long courseInstanceId, SPSite site)
        {
            if (!string.IsNullOrEmpty(userEmail))
            {
                EmailService.SendRegisterConfirmationEmail(userEmail, courseInstanceId, site);
            }
        }
    }
}
